package com.xm.zcf.domain.cart;

import java.text.CollationElementIterator;
import java.util.Collection;
import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2017/4/5.
 */
public class Cart {
    private LinkedHashMap<Integer,CarItem> map=new LinkedHashMap<>();
    private Double total=0.0;

    public LinkedHashMap<Integer, CarItem> getMap() {
        return map;
    }

    public void setMap(LinkedHashMap<Integer, CarItem> map) {
        this.map = map;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }
    public void addCart(CarItem item){
        Integer pid=item.getProduct().getPid();
        if(map.containsKey(pid)){
            CarItem c=map.get(pid);
            c.setCount(c.getCount()+item.getCount());
        }else{
            map.put(pid,item);
        }
        total=total+item.getSubTotal();
    }
    public void removeItem(Integer pid){
        CarItem item=map.remove(pid);
        total=total-item.getSubTotal();
    }
    public void clearCart(){
        map.clear();
        total=0.0;
    }
    public Collection<CarItem> getCartItems(){
        return map.values();
    }
}
